// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
//var net = require('Connection');
var gt = require("protocol");

cc.Class({
    extends: cc.Component,

    properties: {
        numLabelList: [cc.Label],
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.numList = [];
        this.showNum();
        App.socket.registerMsgListener(gt.GC_JOIN_ROOM, this, this.onRcvJoinRoom)
    },

    joinRoom: function (roomId) {
        console.log('join room...', roomId);
        var msgToSend = {};
        msgToSend.m_msgId = gt.CG_JOIN_ROOM;
        msgToSend.m_deskId = roomId;
        msgToSend.m_gps = gt.myLocationStr;
        App.socket.sendMessage(msgToSend);
    },

    onRcvJoinRoom: function (msg) {
        if (msg['m_errorCode'] != 0) {
            cc.director.loadScene("MJPlay");
        }
    },

    start() {

    },
    showNum: function () {
        var len = this.numList.length;
        if (len > 6) len = 6;
        for (var i = 0; i < len; i++) {
            this.numLabelList[i].string = this.numList[i];
        }
        for (var i = len; i < 6; i++) {
            this.numLabelList[i].string = "";
        }
    },

    onClickNum: function (num) {
        if (this.numList.length < 6) {
            this.numList.push(num);
            this.showNum();
        }
        this.confirm();
    },

    onClick_0: function () { this.onClickNum(0); },
    onClick_1: function () { this.onClickNum(1); },
    onClick_2: function () { this.onClickNum(2); },
    onClick_3: function () { this.onClickNum(3); },
    onClick_4: function () { this.onClickNum(4); },
    onClick_5: function () { this.onClickNum(5); },
    onClick_6: function () { this.onClickNum(6); },
    onClick_7: function () { this.onClickNum(7); },
    onClick_8: function () { this.onClickNum(8); },
    onClick_9: function () { this.onClickNum(9); },

    onClickClear: function () {
        this.numList = [];
        this.showNum();
    },

    onClickDelete: function () {
        this.numList.pop();
        this.showNum();
    },

    confirm: function () {
        if (this._checkNum()) {
            var roomId = this._getNum();
            this.joinRoom(roomId);
        }
    },

    _checkNum: function () {
        if (this.numList.length != 6) return false;

        return true;
    },

    _getNum: function () {
        var res = 0;
        for (var i = 0; i < this.numList.length; i++) {
            res = res * 10 + this.numList[i];
        }
        return res;
    },

    onEnable: function () {
        this.node.on('touchstart', function (event) {
            event.stopPropagation();
        });
        this.node.on('touchend', function (event) {
            event.stopPropagation();
        });
    },
    onDisable: function () {
        this.node.off('touchstart', function (event) {
            event.stopPropagation();
        });
        this.node.off('touchend', function (event) {
            event.stopPropagation();
        });
    },
    // update (dt) {},
});
